//
//  RegisterViewController.swift
//  TJJPT
//
//  Created by FENGYL on 2017/5/6.
//  Copyright © 2017年 FENGYL. All rights reserved.
//

import UIKit
import SVProgressHUD
import Alamofire

let kAblumNumber = "0123456789"

class RegisterViewController: UIViewController,UITextFieldDelegate {
    
    @IBOutlet weak var userNameTextField: UITextField!
    @IBOutlet weak var psLabel: UILabel!
    @IBOutlet weak var passwordTextfield: UITextField!
    @IBOutlet weak var confirmPasswordTextfield: UITextField!
    @IBOutlet weak var confirmButton: UIButton!

    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.setUpView()
        
    }
    
    override func viewDidAppear(_ animated: Bool) {
        
        self.navigationController?.setNavigationBarHidden(false, animated: true)
        
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // MARK: - 初始化
    func setUpView() {
        
        // MARK: 设置确认按钮
        self.setUpViewShadow(view: self.confirmButton)
        
        // MARK: 设置用户名textfield
        self.userNameTextField.delegate = self
        self.userNameTextField.placeholder = NSLocalizedString(LOCALIZABLE_STRING_USER_NAME, comment: "")
        
        //左图片
        self.userNameTextField.leftViewMode = .always
        let userNameTextFieldHeight = self.userNameTextField.frame.height
        let userNameLeftView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: userNameTextFieldHeight + 10, height: userNameTextFieldHeight))
        let userNameLeftImageView = UIImageView.init(frame: CGRect.init(x: 10, y: 5, width: userNameTextFieldHeight - 10, height: userNameTextFieldHeight - 10))
        userNameLeftImageView.image = UIImage.init(named: "z_gps_login_user")
        userNameLeftView.addSubview(userNameLeftImageView)
        self.userNameTextField.leftView = userNameLeftView

        
        
        
        
        // MARK: 设置pslabel
        self.psLabel.text = NSLocalizedString(LOCALIZABLE_STRING_USER_NAME_LENGTH_TIPS, comment: "")
        
        
        
        
        // MARK: 设置passwordTextfield
        self.passwordTextfield.delegate = self
        self.passwordTextfield.placeholder = NSLocalizedString(LOCALIZABLE_STRING_PASSWORD, comment: "")
        
        //左图片
        self.passwordTextfield.leftViewMode = .always
        let passwordTextfieldHeight = self.passwordTextfield.frame.height
        let passwordLeftView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: passwordTextfieldHeight + 10, height: passwordTextfieldHeight))
        let passwordLeftImageView = UIImageView.init(frame: CGRect.init(x: 10, y: 5, width: passwordTextfieldHeight - 10, height: passwordTextfieldHeight - 10))
        passwordLeftImageView.image = UIImage.init(named: "z_gps_login_lock")
        passwordLeftView.addSubview(passwordLeftImageView)
        self.passwordTextfield.leftView = passwordLeftView
        
        
        
        
        
        
        
        
        // MARK: 设置confirmPasswordTextfield
        self.confirmPasswordTextfield.delegate = self
        self.confirmPasswordTextfield.placeholder = NSLocalizedString(LOCALIZABLE_STRING_CONFIRM_PASSWORD, comment: "")
        
        //左图片
        self.confirmPasswordTextfield.leftViewMode = .always
        let confirmPasswordTextfieldHeight = self.confirmPasswordTextfield.frame.height
        let confirmPasswordLeftView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: confirmPasswordTextfieldHeight + 10, height: confirmPasswordTextfieldHeight))
        let confirmPassworImageView = UIImageView.init(frame: CGRect.init(x: 10, y: 5, width: confirmPasswordTextfieldHeight - 10, height: confirmPasswordTextfieldHeight - 10))
        confirmPassworImageView.image = UIImage.init(named: "z_gps_login_lock")
        confirmPasswordLeftView.addSubview(confirmPassworImageView)
        self.confirmPasswordTextfield.leftView = confirmPasswordLeftView
        
        
        
        
        
        
        // MARK: 设置confirmButton
        self.confirmButton.setTitle(NSLocalizedString(LOCALIZABLE_STRING_CONFIRM, comment: ""), for: .normal)
        
        
        
    }
    
    // MARK: - 设置阴影
    func setUpViewShadow(view: AnyObject){
        
        view.layer.shadowOpacity = 0.4
        view.layer.shadowColor = UIColor.black.cgColor
        view.layer.shadowOffset = CGSize(width: 1, height: 1)
        
    }
    
    // MARK: - 判断是否数字
    func isPurnInt(string: String) -> Bool {
        
        let scan: Scanner = Scanner(string: string)
        
        var val:Int = 0
        
        return scan.scanInt(&val) && scan.isAtEnd
        
    }
    
    
    
    // MARK: - 确认按钮点击事件
    @IBAction func confirmButtonEvent(_ sender: UIButton) {
        
        // MARK: 判断是否11位
        if self.userNameTextField.text?.characters.count != 11 {
            
            SVProgressHUD.showError(withStatus: NSLocalizedString(LOCALIZABLE_STRING_USER_NAME_LENGTH_ERROR, comment: ""))
            
            SVProgressHUD.dismiss(withDelay: 1)
            
            return
            
        }
        
        // MARK: 判断密码是否大于等于6
        if !((self.passwordTextfield.text?.characters.count)! > 5 || (self.confirmPasswordTextfield.text?.characters.count)! > 5) {
            
            SVProgressHUD.showError(withStatus: NSLocalizedString(LOCALIZABLE_STRING_PASSWORD_TOO_SHORT, comment: ""))
            
            SVProgressHUD.dismiss(withDelay: 1)
            
            return
            
        }else{
            
            // MARK: 判断两个密码是否相同
            if self.passwordTextfield.text != self.confirmPasswordTextfield.text{
                
                SVProgressHUD.showError(withStatus: NSLocalizedString(LOCALIZABLE_STRING_CONFIRM_PASSWORD_DIFFERENT, comment: ""))
                
                SVProgressHUD.dismiss(withDelay: 1)
                
                return
                
            }
            
        }
        
        // MARK: 查询用户名是否可用(全部返回null,没卵用)
//        NetworkCommand.existAccountName(userName: self.userNameTextField.text!, success: { response in
//            
//            if response.isEmpty{
//                
//                SVProgressHUD.showError(withStatus: NSLocalizedString(LOCALIZABLE_STRING_REQUEST_FAILURE, comment: ""))
//                
//                SVProgressHUD.dismiss(withDelay: 1)
//                
//            }else{
//                
//                let code:NSInteger = response[NETWORK_COMMAND_KEY_CODE] as! NSInteger
//                
//                if code > 0{
//                    
//                    SVProgressHUD.showError(withStatus: NSLocalizedString(LOCALIZABLE_STRING_USER_NAME_REGISTERED, comment: ""))
//                    
//                    SVProgressHUD.dismiss(withDelay: 1)
//                    
//                }else if code < 0{
//                    
//                    SVProgressHUD.showError(withStatus: NSLocalizedString(LOCALIZABLE_STRING_REQUEST_FAILURE, comment: ""))
//                    
//                    SVProgressHUD.dismiss(withDelay: 1)
//                    
//                }else if code == 0{
//                    
//                    
//                    
//                }
//                
//            }
//            
//            
//        }) { Error in
//            
//            SVProgressHUD.showError(withStatus: NSLocalizedString(LOCALIZABLE_STRING_REQUEST_TIMED_OUT, comment: ""))
//            
//            SVProgressHUD.dismiss(withDelay: 1)
//            
//        }
        
        
        
        
        // MARK: 创建账号
        NetworkCommand.createNetAccount(userName: self.userNameTextField.text!, password: self.passwordTextfield.text!, success: { (response) in
            
            SVProgressHUD.showSuccess(withStatus: NSLocalizedString(LOCALIZABLE_STRING_REGISTER_SUCCESS, comment: ""))
            
            SVProgressHUD.dismiss(withDelay: 1)
            
            self.navigationController?.popViewController(animated: true)
            
            return
            
        }) { (Error) in
            
            SVProgressHUD.showError(withStatus: NSLocalizedString(LOCALIZABLE_STRING_REGISTER_FAILURE, comment: ""))
            
            SVProgressHUD.dismiss(withDelay: 1)
            
            return
            
        }
        
    }
    
    
    
    
    
    
    // MARK: - textfieldDelegate
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        
        // MARK: 限制用户名输入长度
        if textField == self.userNameTextField {
            
            if range.length == 1 {
                
                return true
                
            }
            
            if isPurnInt(string: string){
                
                if (textField.text?.characters.count)! < 11 {
                    
                    return true
                    
                }else{
                    
                    return false
                    
                }
                
            }else{
                
                return false
                
            }
            
        }
        
        // MARK: 限制密码输入长度
        if textField == self.passwordTextfield || textField == self.confirmPasswordTextfield {
            
            if (textField.text?.characters.count)! > 31{
                
                return false
                
            }
            
        }
        
        return true
        
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
